package com.lc.pianostudent.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.alipay.OrderInfoUtil2_0;
import com.lc.pianostudent.alipay.PayAction;
import com.lc.pianostudent.alipay.PayResult;
import com.lc.pianostudent.conn.Conn;
import com.lc.pianostudent.conn.PostJournalIndex;
import com.lc.pianostudent.conn.PostOrderIndex;
import com.lc.pianostudent.conn.PostUserTeacher;
import com.lc.pianostudent.conn.PostUserVipUser;
import com.lc.pianostudent.widget.BalanceCheckView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/6/25.
 */

public class VipActivity extends BaseActivity implements View.OnClickListener{

    @BoundView(R.id.wx_iamge) private BalanceCheckView wx;
    @BoundView(R.id.zfb_iamge) private BalanceCheckView zfb;
    @BoundView(value = R.id.wx_layout,isClick = true) private RelativeLayout wxLayouy;
    @BoundView(value = R.id.zfb_layout,isClick = true) private RelativeLayout zfbLayouy;
    @BoundView(value = R.id.confirm_pay,isClick = true) private LinearLayout confirm_pay;
    @BoundView(R.id.head_vip) private CircleImageView head_vip;
    @BoundView(R.id.name_vip) private TextView name_vip;
    @BoundView(R.id.person_vip) private TextView person_vip;
    @BoundView(R.id.stop_time_vip) private TextView stop_time_vip;
    @BoundView(value = R.id.rl_title_back, isClick = true)
    private RelativeLayout back_layout;
    private PayAction payAction;
    @BoundView(R.id.money_text) private TextView money_text;
    private static final int SDK_PAY_FLAG = 1001;
    public static final String RSA_PRIVATE ="MIIEvAIBADANBgkqhkiG9w0BAQEFAAS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Cc3hJ21nh0kfjXHnL43MvoSBVPEsbAhp//zIvaCKPomHUtbItplom+KyZWkAQPtBquz+yWBqAEQX/ldPaxDrQta5gOV6QLiYNuo41nicVyiCbjMeSfNy5XTT4GfUXpURq3v9DIpRthBAIa0gH89tcKbt9AoGAA90GGyH7Mg6iy2/I2njrkKcNt4HSgdY/5PxkAxdhSjAeZzjReUbVYeppKdyrVd4MfFgrJUz7IEvzfPlkrBT8O+x0qiA1wxe/FQA8KhOWFHQh+PkPWUkC/DJyhbG11jdO5+SgSAT/rGe09w9WUYq/tW2B/gWUmtZC9HQPgEoh6F0CgYA+G283r3X33RPGxJWpd9zM/qQ0RWOxhgcCxw//NE0kXsxRPF3xnP+s0JrE+fiyVsSTgjLZb2zV06EFDtsJZ04CNYdReoHGGVjYiC+NwfwVZfeMduiZLK3sX8rV7iKGFkLXJLO02aq+m0TBAyiGVCI/WhogTGKNkV8IjQaLXKjb+A==";
    public static final String APPID = "2018071960671521";
//    public static final String RSA_PRIVATE ="MIIEogIBAAKCAQEAub2sIYNyLBr58iIU9Gk5GCGcz0RcWPGukBc0IUnyJtmhUwfC1P2tt9UV1CFT9QmvV2tv1QXQMdsTmaGGUm/GvpoUtwG6bJJUp7bMnCgwm4/iNlDmbHaXr+ByBUEAD8SknMIYCZhc1QF0V9hkKpjpPTujwoa/0xcAjJkfI+ro0Emg1/wnlcAEj5hrRuTYbW+RekCGBVjcvlYy/3UHyMMiAIvA+WuE/JOISmT2hWO1kvb1vhKO6bDjTOzLDlXEUb8gOSqmJ86YNNo2eMISRpKKVLTQvwEXqI59Q8i/IHej2k4PItKQbKp9pLvXUybXLMxojBLP8HZ5ge6DYhljVXacjQIDAQABAoIBADvdkVQ/7JWgoDzVlVFtdLR9aPA5BVa5ltzkTa4JCl5ROLjtQHOry8j7igyULV5GH9iaTTIorIDVLo0OzZ9WXYVjXhfaG78Qru5bcblHDtuk1XFSQ+JjmuJaAgYFfEKYy8GmoV6+V/VjgZ8qpKZCILLWvSJekgc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Teacher = new PostUserVipUser(new AsyCallBack<PostUserVipUser.PostUserVipUserInfo>() {
        @Override
        public void onSuccess(String toast, int type, Object o, PostUserVipUser.PostUserVipUserInfo postUserVipUserInfo) throws Exception {
            stop_time_vip.setText("VIP权限到期时间为"+postUserVipUserInfo.time);
            money_text.setText("如需付费"+postUserVipUserInfo.money+"元/年，请按一下操作");
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }
    });
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SDK_PAY_FLAG:
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    //同步获取结果
                    String resultInfo = payResult.getResult();
                    Log.i("Pay", "Pay:" + resultInfo);
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        Toast.makeText(VipActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(VipActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };


    private PostOrderIndex postJournalIndex = new PostOrderIndex(new AsyCallBack<PostOrderIndex.PostOrderIndexInfo>() {

        @Override
        public void onSuccess(String toast, int type, Object o, PostOrderIndex.PostOrderIndexInfo postOrderIndexInfo) throws Exception {

            if(zfb.isCheck()){
                payAction.pay(postOrderIndexInfo.money,postOrderIndexInfo.status,"琴之语充值",postOrderIndexInfo.order_number);
            }else if(wx.isCheck()){
                BaseApplication.WeiXinPay.setNotifyUrl(Conn.SERVICE+Conn.WECHAT_NOTIFYURL).pay("充值金额","1",postOrderIndexInfo.order_number,((int) (Float.valueOf(postOrderIndexInfo.money + "") * 1000/10) + ""));

            }
//            BaseApplication.WeiXinPay.setNotifyUrl(Conn.SERVICE+Conn.WECHAT_NOTIFYURL).pay("充值金额","1",postOrderIndexInfo.order_number,((int) (Float.valueOf(0.01 + "") * 1000/10) + ""));


        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }

    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip);


//        setBackTrue();
        initView();
    }

    private void initView(){
        GlideLoader.getInstance().get("", BaseApplication.BasePreferences.readHeadImg(),head_vip,R.mipmap.new_head);
        name_vip.setText(BaseApplication.BasePreferences.readName());
        person_vip.setText(BaseApplication.BasePreferences.readPerson());

        postUserTeacher.user_id = BaseApplication.BasePreferences.readUID();
        postUserTeacher.execute();
        setTitleName("立即支付");
        wx.setCheck(true);

        payAction = new PayAction(this) {
            @Override
            protected void onSuccess() {
               if(getIntent().getStringExtra("from").equals("1")){
                   UtilToast.show("支付成功，请重新登录");
                   BaseApplication.BasePreferences.setIsLogin(false);
                   finish();

                   if (NavigationActivity.onClickProduct != null) {
                       NavigationActivity.onClickProduct.setOnClick();
                   }
               }else {
                   finish();
               }
            }

            @Override
            protected void onEnd() {
                Log.e("支付宝回调:","回调失败");
            }
        };
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.wx_layout:

                wx.setCheck(true);
                zfb.setCheck(false);
                break;

            case R.id.zfb_layout:

                wx.setCheck(false);
                zfb.setCheck(true);
                break;

            case R.id.confirm_pay:
                   postJournalIndex.user_id = BaseApplication.BasePreferences.readUID();
                   postJournalIndex.execute();
                break;
            case R.id.rl_title_back:

                if(getIntent().getStringExtra("from").equals("1")){
                    BaseApplication.BasePreferences.setIsLogin(false);
                    BaseApplication.INSTANCE.finishAllActivity();
                }else {
                    finish();
                }
                break;
        }
    }

    //返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {

            if(getIntent().getStringExtra("from").equals("1")){
                BaseApplication.BasePreferences.setIsLogin(false);
                BaseApplication.INSTANCE.finishAllActivity();
            }else {
                finish();
            }
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

}
